//
//  LevelSelectLayer.m
//  SpeedTap
//
//  Created by Chi Ho Chiu on 20/8/11.
//  Copyright 2011 Individual. All rights reserved.
//

#import "LevelSelectLayer.h"

@implementation GuGuSprite
@synthesize currentLocationTag;
@end

@implementation LevelSelectLayer
@synthesize delegate;

-(id) init
{
    if( (self = [super init]) )
    {
		self.isTouchEnabled = YES;
		
		//Add background image
        CCSprite *bgSprite = [CCSprite spriteWithFile:@"map.png"];
        [bgSprite setPosition:CGPointMake(240,160)];
        [self addChild:bgSprite z:0];

		
        //Create the back Button
        CCLabelBMFont *backFont = [CCLabelBMFont labelWithString:@"Back" fntFile:@"Dakota_white_24.fnt"];
        CCMenuItem *backLabel = [CCMenuItemLabel itemWithLabel:backFont target:self selector:@selector(onMenuItemClicked:)];
        [backLabel setTag:kLEVEL_SELECT_TAG_BACK];
        
        CCMenu *backMenu = [CCMenu menuWithItems:backLabel, nil];
        [backMenu setPosition: ccp( [backLabel contentSize].width/2, [backLabel contentSize].height/2 )];
        [self addChild:backMenu];
		
		//Add Hong Kong Sprite
		CCSprite *hkSprite = [CCSprite node];
		[hkSprite setOpacity:128];
		[hkSprite setColor:ccc3(0, 250, 0)];
		[hkSprite setTextureRect:CGRectMake(0, 0, 68, 49)];
		[hkSprite setPosition:CGPointMake(171, 79)];
		[self addChild:hkSprite z:1 tag:kLEVEL_SELECT_TAG_HONG_KONG];
		
		//Add Taiwan Sprite
		CCSprite *twSprite = [CCSprite node];
		[twSprite setOpacity:128];
		[twSprite setColor:ccc3(0, 250, 0)];
		[twSprite setTextureRect:CGRectMake(0, 0, 62, 43)];
		[twSprite setPosition:CGPointMake(309, 68)];
		[self addChild:twSprite z:1 tag:kLEVEL_SELECT_TAG_TAIWAN];
		
		//Add Japan Sprite
		CCSprite *jpSprite = [CCSprite node];
		[jpSprite setOpacity:128];
		[jpSprite setColor:ccc3(0, 250, 0)];
		[jpSprite setTextureRect:CGRectMake(0, 0, 75, 57)];
		[jpSprite setPosition:CGPointMake(425, 105)];
		[self addChild:jpSprite z:1 tag:kLEVEL_SELECT_TAG_JAPAN];
		
		//Add Korea Sprite
		CCSprite *koreaSprite = [CCSprite node];
		[koreaSprite setOpacity:128];
		[koreaSprite setColor:ccc3(0, 250, 0)];
		[koreaSprite setTextureRect:CGRectMake(0, 0, 69, 53)];
		[koreaSprite setPosition:CGPointMake(352, 224)];
		[self addChild:koreaSprite z:1 tag:kLEVEL_SELECT_TAG_KOREA];
		
		//Add China Sprite
		CCSprite *chinaSprite = [CCSprite node];
		[chinaSprite setOpacity:128];
		[chinaSprite setColor:ccc3(0, 250, 0)];
		[chinaSprite setTextureRect:CGRectMake(0, 0, 61, 40)];
		[chinaSprite setPosition:CGPointMake(135, 185)];
		[self addChild:chinaSprite z:1 tag:kLEVEL_SELECT_TAG_CHINA];
		
		//Add Gugu Sprite
		GuGuSprite *guguSprite = [GuGuSprite node];
		[guguSprite setOpacity:128];
		[guguSprite setColor:ccc3(0, 0, 255)];
		[guguSprite setTextureRect:CGRectMake(0, 0, 50, 50)];
		[guguSprite setPosition:[hkSprite position]]; 		//TODO: Get Gugu initial position
		[guguSprite setCurrentLocationTag:kLEVEL_SELECT_TAG_HONG_KONG];	//TODO: Get initial location tag
		[self addChild:guguSprite z:1 tag:kLEVEL_SELECT_TAG_GUGU];
        
    }
    return self;
}

-(void) dealloc
{
    [super dealloc];   
}

-(void) onMenuItemClicked:(CCMenuItem *)sender
{
    if(sender.tag == kLEVEL_SELECT_TAG_BACK)
    {
        [delegate onBackSelected];
    }
}

#pragma mark - Action Callback
-(void) finishedMoving:(id)sender
{
	//TODO: Any operation after Gugu moved
}

#pragma mark - Member Function
-(void) startMovement:(ELevelSelectTag)destination
{
	//Get GuGu Sprite
	GuGuSprite *guguSprite = (GuGuSprite *)[self getChildByTag:kLEVEL_SELECT_TAG_GUGU];
		
	//Get source
	ELevelSelectTag source = guguSprite.currentLocationTag;
		
	//Get number of country diff and go which way
	int diff = 0;
	bool bGoForward = true;
	if(destination >= source)	//Go forward
	{
		diff = destination - source;
		bGoForward = true;
	}
	else	//Go backward
	{
		diff = source - destination;
		bGoForward = false;
	}

	//Create the CCMoveTo
	NSMutableArray *actionArray = [NSMutableArray arrayWithCapacity:diff];
	
	for(int i=0; i < diff; i++)
	{
		ELevelSelectTag target = -1;
		if(bGoForward)
		{
			target = guguSprite.currentLocationTag + 1;	//Set the target
			guguSprite.currentLocationTag += 1;			//Update guguSprite currentLocation
		}
		else
		{
			target = guguSprite.currentLocationTag - 1;
			guguSprite.currentLocationTag -= 1;
		}
		
		//Create object
		CCMoveTo *moveTo = [CCMoveTo actionWithDuration:2 position:[ [self getChildByTag:target] position] ];
		[actionArray addObject:moveTo];

	}

	//Create the callback function once finished moving
	CCCallFuncN *callback = [CCCallFuncN actionWithTarget:self selector:@selector(finishedMoving:)];
	[actionArray addObject:callback];
	
	//Run action
	[guguSprite runAction:[CCSequence actionsWithArray:actionArray]];
}

#pragma mark - Touch Event
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* touch = [touches anyObject];
	CGPoint touchLocation =[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];

	for(int iTag = kLEVEL_SELECT_TAG_HONG_KONG; iTag <= kLEVEL_SELECT_TAG_CHINA; iTag++)
	{
		if( CGRectContainsPoint([[self getChildByTag:iTag] boundingBox], touchLocation) )
		{
			GuGuSprite *guguSprite = (GuGuSprite *)[self getChildByTag:kLEVEL_SELECT_TAG_GUGU];
			if(guguSprite.currentLocationTag == iTag)		//If tapped in the same country, enter the level
			{
				if( [guguSprite numberOfRunningActions] == 0)	//Allow Selection if sprite is not moving
				{
					[delegate onLevelSelected:iTag];	//Start game
				}
			}
			else
			{
				//Check if gugu is moving
				if([guguSprite numberOfRunningActions] == 0)
				{
					[self startMovement:iTag];	//Move to destination only if gugu is not moving
				}

			}
			
			break;
		}
	}	
}

@end
